<?php
/**
 * The framework's class loader. Autoload is switched on for convenience.
 */
class Loader {
    /**
     * Load a class.
     * @param string $class
     */
    public static function loadClass($class) {
        if(class_exists($class, false) || interface_exists($class, false)) {
            return;
        }
        $class = str_replace('_', '/', $class);
        require_once($class . '.php');
    }

    /**
     * The autoloading function. Loader::loadClass() is called to actually load the class.
     * @see Loader::loadClass()
     * @param string $class
     */
    public static function autoload($class) {
        try {
            self::loadClass($class);
            return $class;
        } catch(Exception $e) {
            return false;
        }
    }

    /**
     * Register the autoloader.
     * @param string $class
     */
    public static function registerAutoload($class = 'Loader') {
        if(!function_exists('spl_autoload_register')) {
            die('No autoload');
        }
        self::loadClass($class);
        $methods = get_class_methods($class);
        if(!in_array('autoload', (array)$methods)) {
            die('No autoload method in ' . $class);
        }
        spl_autoload_register(array($class, 'autoload'));
    }
}
?>
